//
//  GMQustionCell.m
//  Teecloud
//
//  Created by 小熊 on 2018/10/22.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMQustionCell.h"
#import "TZImagePickerController.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>
#import "GMPhotoCell.h"
#import "GMPlayerVoideCell.h"
#import "GMAttachListModel.h"
#import "GMLookPDFVC.h"
@interface GMQustionCell ()<UICollectionViewDataSource,UICollectionViewDelegate,TZImagePickerControllerDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>
/**提问视图*/
@property (weak, nonatomic) IBOutlet UIView *qustionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *qustionHight;
@property (weak, nonatomic) IBOutlet UIImageView *qustionImg;
@property (weak, nonatomic) IBOutlet UILabel *qustionName;
@property (weak, nonatomic) IBOutlet UILabel *qustionTime;
@property (weak, nonatomic) IBOutlet UILabel *questionContent;
@property (weak, nonatomic) IBOutlet UITableView *voiceView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *voidHight;
@property (weak, nonatomic) IBOutlet UICollectionView *ImgsView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgHight;
@property (weak, nonatomic) IBOutlet UIView *postionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *postionHight;
@property (weak, nonatomic) IBOutlet UILabel *postionTime;


/**回答视图*/
@property (weak, nonatomic) IBOutlet UIView *awserView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *awserHight;
@property (weak, nonatomic) IBOutlet UIImageView *awserImg;
@property (weak, nonatomic) IBOutlet UILabel *awserName;
@property (weak, nonatomic) IBOutlet UILabel *awserTime;
@property (weak, nonatomic) IBOutlet UILabel *awserContent;
@property (weak, nonatomic) IBOutlet UITableView *awserVoidView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *awserVoidHight;
@property (weak, nonatomic) IBOutlet UICollectionView *awserImgView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *awserImgHight;


@end
@implementation GMQustionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _ImgsView.backgroundColor = kWhiteColor;
    _ImgsView.dataSource = self;
    _ImgsView.scrollEnabled = NO;
    _ImgsView.delegate = self;
    [_ImgsView registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
    _voiceView.dataSource = self;
    _voiceView.delegate = self;
    _voiceView.scrollEnabled = NO;
    [_voiceView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];
    _awserVoidView.dataSource = self;
    _awserVoidView.delegate = self;
    [_awserVoidView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];
    _awserImgView.dataSource = self;
    _awserImgView.delegate = self;
    [ _awserImgView registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTapChange:)];
    [self.postionView addGestureRecognizer:tap];
}
//点击
-(void)doTapChange:(UITapGestureRecognizer *)sender{
    self.playTap();
    
}
-(void)setBaseModel:(GMBaseQuestionModel *)baseModel{
    _baseModel = baseModel;
    [self.qustionImg yy_setImageWithURL:[NSURL URLWithString:baseModel.Question.HeadUrl] placeholder:DefealtImg];
    self.qustionName.text = baseModel.Question.Name;
    self.qustionTime.text = baseModel.Question.Time;
    self.questionContent.text = baseModel.Question.Content;
    CGFloat contentHight = [self.questionContent getLableSizeWithMaxWidth:(DEVICE_WIDTH-88)].height;
    if (baseModel.Question.VoiceList.count == 0) {
        self.voiceView.hidden = YES;
        self.voidHight.constant = 0;
    }else{
        self.voiceView.hidden = NO;
        self.voidHight.constant = 30*baseModel.Question.VoiceList.count;
        [self.voiceView reloadData];
    }
    if (baseModel.Question.ImgFileList.count == 0) {
        self.ImgsView.hidden = YES;
        self.imgHight.constant = 0;
    }else{
        self.ImgsView.hidden = NO;
        self.imgHight.constant =60;
        [self.ImgsView reloadData];
    }
    if (baseModel.Question.Position) {
        self.postionView.hidden = NO;
        self.postionHight.constant = 30;
        self.postionTime.text = baseModel.Question.PositionStr;
    }else{
        self.postionView.hidden = YES;
        self.postionHight.constant = 0;
    }
    self.qustionHight.constant = 50 +2+contentHight+2+self.voidHight.constant+2 +self.imgHight.constant+2+self.postionHight.constant;
    if (baseModel.Answer) {
        self.awserView.hidden =NO;
        [self.awserImg yy_setImageWithURL:[NSURL URLWithString:baseModel.Answer.HeadUrl] placeholder:DefealtImg];
        self.awserName.text = baseModel.Answer.Name;
        self.awserTime.text = baseModel.Answer.Time;
        self.awserContent.text = baseModel.Answer.Content;
         CGFloat awcontentHight = [self.awserContent getLableSizeWithMaxWidth:(DEVICE_WIDTH-88)].height;
        if (baseModel.Answer.VoiceList.count == 0) {
            self.awserVoidView.hidden =YES;
            self.awserVoidHight.constant = 0;
        }else{
            self.awserVoidView.hidden =NO;
            self.awserVoidHight.constant = baseModel.Answer.VoiceList.count *30;
            [self.awserVoidView reloadData];
        }
        if (baseModel.Answer.ImgFileList.count == 0) {
            self.awserImgView.hidden = YES;
            self.awserImgHight.constant = 0;
        }else{
            self.awserImgView.hidden = NO;
            self.awserImgHight.constant = 60;
            [self.awserImgView reloadData];
        }
        self.awserHight.constant = 50+2 +awcontentHight+2+ self.awserVoidHight.constant+2+self.awserImgHight.constant+5;
    }else{
        self.awserView.hidden =YES;
        self.awserHight.constant =  0;
    }
    baseModel.cellHight = self.qustionHight.constant + self.awserHight.constant+4;
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual:self.awserImgView]) {
           return self.baseModel.Answer.ImgFileList.count;
    }else{
           return self.baseModel.Question.ImgFileList.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([collectionView isEqual:self.awserImgView]){
        GMPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMPhotoCell" forIndexPath:indexPath];
        GMAttachListModel *model= self.baseModel.Answer.ImgFileList[indexPath.row];
        if ([model.FileType isEqualToNumber:@(3)]) {
            [cell.photoImg yy_setImageWithURL:[NSURL URLWithString:model.Url] placeholder:DefealtImg];
        }else if ([model.FileType isEqualToNumber:@(4)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-pdf"];
        }else if ([model.FileType isEqualToNumber:@(5)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-ppt"];
        }else if ([model.FileType isEqualToNumber:@(6)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-word"];
        }else if ([model.FileType isEqualToNumber:@(7)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-excel"];
        }else if ([model.FileType isEqualToNumber:@(8)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-rar"];
        }else if ([model.FileType isEqualToNumber:@(9)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-zip"];
        }else{
            cell.photoImg.image =[UIImage imageNamed:@"doc-other"];
        }
        cell.delectBtn.hidden=YES;
        return cell;
    }else{
        GMPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMPhotoCell" forIndexPath:indexPath];
        GMAttachListModel *model= self.baseModel.Question.ImgFileList[indexPath.row];
        if ([model.FileType isEqualToNumber:@(3)]) {
            [cell.photoImg yy_setImageWithURL:[NSURL URLWithString:model.Url] placeholder:DefealtImg];
        }else if ([model.FileType isEqualToNumber:@(4)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-pdf"];
        }else if ([model.FileType isEqualToNumber:@(5)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-ppt"];
        }else if ([model.FileType isEqualToNumber:@(6)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-word"];
        }else if ([model.FileType isEqualToNumber:@(7)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-excel"];
        }else if ([model.FileType isEqualToNumber:@(8)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-rar"];
        }else if ([model.FileType isEqualToNumber:@(9)]){
            cell.photoImg.image =[UIImage imageNamed:@"doc-zip"];
        }else{
            cell.photoImg.image =[UIImage imageNamed:@"doc-other"];
        }
        cell.delectBtn.hidden=YES;
        return cell;
    }
   
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((DEVICE_WIDTH-88-10-10-40)/5, (DEVICE_WIDTH-88-10-10-40)/5);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 0, 10,4);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if ([collectionView isEqual:self.awserImgView]){
        /**放大图片*/
        GMAttachListModel *fileModel= self.baseModel.Answer.ImgFileList[indexPath.row];
        if ([fileModel.FileType isEqualToNumber:@(3)]) {
            GMPhotoCell * cell = (GMPhotoCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
            NSMutableArray *items = @[].mutableCopy;
            NSInteger count = self.baseModel.Answer.ImgFileList.count;
            for (int i = 0; i < count; i++) {
                GMAttachListModel *model= self.baseModel.Answer.ImgFileList[i];
                if ([model.FileType isEqualToNumber:@(3)]) {
                    UIImageView *imgV = cell.photoImg;
                    KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
                    [items addObject:item];
                }
            }
            KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
            browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
            browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
            browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
            browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
            browser.bounces = NO;
            [browser showFromViewController:self.baseVC];
        }else{
            GMLookPDFVC *preview = [[GMLookPDFVC alloc] init];
            preview.model = fileModel;
            [self.baseVC.navigationController pushViewController:preview animated:YES];
        }
    }else{
        /**放大图片*/
        GMAttachListModel *fileModel= self.baseModel.Question.ImgFileList[indexPath.row];
        if ([fileModel.FileType isEqualToNumber:@(3)]) {
            GMPhotoCell * cell = (GMPhotoCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
            NSMutableArray *items = @[].mutableCopy;
            NSInteger count = self.baseModel.Question.ImgFileList.count;
            for (int i = 0; i < count; i++) {
                GMAttachListModel *model= self.baseModel.Question.ImgFileList[i];
                if ([model.FileType isEqualToNumber:@(3)]) {
                    UIImageView *imgV = cell.photoImg;
                    KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
                    [items addObject:item];
                }
            }
            KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
            browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
            browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
            browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
            browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
            browser.bounces = NO;
            [browser showFromViewController:self.baseVC];
        }else{
            GMLookPDFVC *preview = [[GMLookPDFVC alloc] init];
            preview.model = fileModel;
            [self.baseVC.navigationController pushViewController:preview animated:YES];
        }
    }
   
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.awserVoidView]) {
         return self.baseModel.Answer.VoiceList.count;
    }else{
         return self.baseModel.Question.VoiceList.count;
    }
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.awserVoidView]) {
        GMPlayerVoideCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMPlayerVoideCell" forIndexPath:indexPath];
        GMVoiceListModel *model = self.baseModel.Answer.VoiceList[indexPath.row];
        cell.model = model;
        kWeakObject(self)
        cell.readBlock = ^{
            weakself.hasReadBlock(indexPath.row, model.Id);
        };
        return cell;
    }else{
        GMPlayerVoideCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMPlayerVoideCell" forIndexPath:indexPath];
        GMVoiceListModel *model = self.baseModel.Question.VoiceList[indexPath.row];
        cell.model = model;
        kWeakObject(self)
        cell.readBlock = ^{
            weakself.hasReadBlock(indexPath.row, model.Id);
        };
        return cell;
    }
   
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}


@end
